# Maintainer: James Duley <jagduley gmail>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=2.7
pkgrel=4
arch=('any')
license=('LGPL')
url="http://osgearth.org"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-gdal")
options=('staticlibs' 'strip')
source=("https://github.com/gwaldron/osgearth/archive/${_realname}-${pkgver}.tar.gz"
        "0001-Fix-mingw-debug-build.patch"
        "0002-Forced-include-headers-for-Qt5-moc-on-files-without-.patch"
        "0003-Build-fix-against-OSG-3.4.0-and-OSG-3.5.0.patch"
        "0004-Fixed-debug-library-names.patch"
        "0005-Fixed-crash-due-to-probable-GCC-5.3.0-optimizer-bug.patch"
        "0006-Fix-find-minizip.patch")
sha256sums=('cf973b664aeb79f70e48f5cd02ba670069ec273d71fe541604ed5b328d956d83'
            '60df329759281799941a3f951227c5c847bf76062a8fa4891311095f5f593dc2'
            '66d40b0a93cafcad48341eb6d70ee6a72568487d1978500e6f99d7c85ff65e32'
            '6f6cfcb9899618c473660619dd94fc006799151001cf9376ef30a20538aaf0a5'
            'e6560d913678975af939a09a3671636aa731c8aab5bfb09b7e2def2195196de2'
            'ef451fa5a21f247ac1fe048e7990c59ed4f4be780af4f1ede891007a68b60603'
            '4818d7004bc64585d9d2efb4b4d55f29aff4e0576ddf1db2f6192af7d76f4dc5')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-Fix-mingw-debug-build.patch
  patch -p1 -i ${srcdir}/0002-Forced-include-headers-for-Qt5-moc-on-files-without-.patch
  patch -p1 -i ${srcdir}/0003-Build-fix-against-OSG-3.4.0-and-OSG-3.5.0.patch
  patch -p1 -i ${srcdir}/0004-Fixed-debug-library-names.patch
  patch -p1 -i ${srcdir}/0005-Fixed-crash-due-to-probable-GCC-5.3.0-optimizer-bug.patch
  patch -p1 -i ${srcdir}/0006-Fix-find-minizip.patch
}

build() {
  _pkgver=${_realname}-${pkgver} # How it's tagged in git

  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir}
    pushd ${builddir}
      [[ "${builddir%-${MINGW_CHOST}}" == "Release" ]] && {
        local _opts=""
      } || {
        local _opts=""
      }

      msg "Building ${builddir%-${MINGW_CHOST}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"MSYS Makefiles" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MINGW_CHOST}} \
        ${_opts} \
        ../${_realname}-${_pkgver}

      make
    popd
  done
}

package_release() {
  depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
           "${MINGW_PACKAGE_PREFIX}-gdal")
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"

  cd Release-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver"
           "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  options=('staticlibs' '!strip')
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (debug) (mingw-w64)"

  cd Debug-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-osgearth() {
  package_release
}

package_mingw-w64-i686-osgearth-debug() {
  package_debug
}

package_mingw-w64-x86_64-osgearth() {
  package_release
}

package_mingw-w64-x86_64-osgearth-debug() {
  package_debug
}
